package com.mz.ozy.activity.other;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.constants.Constants;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GameDetailActivity extends BaseActivity {

    @BindView(R.id.ll_H5Page)
    LinearLayout mLlH5Page;
    @BindView(R.id.webview)
    WebView mWebview;
    private String gameCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_detail);
        ButterKnife.bind(this);
        gameCode = getIntent().getStringExtra("code");
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        try {
            Class<?> clazz = webSettings.getClass();
            Method method = clazz.getMethod(
                    "setAllowUniversalAccessFromFileURLs", boolean.class);
            if (method != null) {
                method.invoke(webSettings, true);
            }
        } catch (IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

//        mWebview.loadUrl(Constants.Http.game_URL + SPUtils.getInstance(this).getString(Constants.SPName.GAMECODE, ""));
//        mWebview.loadUrl(Constants.Http.game_URL + SPUtils.getInstance(this).getString(Constants.SPName.GAMECODE, ""));
        mWebview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mWebview.loadUrl(Constants.Http.game_URL + gameCode);
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("backapp://h5back")) {
                    finish();
                } else {
                    mWebview.loadUrl(url);
                }
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyWebView();
    }

    public void destroyWebView() {
        mWebview.destroy();
    }
}
